package com.example.controller;

import com.example.common.api.CommonResult;
import com.example.domain.TokenVO;
import com.example.service.AuthorizeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("auth-token")
public class AuthorizeController {
    @Resource
    private AuthorizeService authorizeService;

    @Value("${jwt.token-header}")
    private String tokenHeader;

    @Value("${jwt.token-type}")
    private String tokenType;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public CommonResult<TokenVO> login(@RequestParam("username") String username,
                                       @RequestParam("password") String password) {
        String token = authorizeService.login(username, password);
        return CommonResult.success(new TokenVO(tokenHeader, tokenType, token), "保存成功");
    }

    /**
     * 用户注销
     */
    @PostMapping("/logout")
    public CommonResult<String> logout() {
        return CommonResult.success(null, "注销成功");
    }
}
